package com.pubinfo.passbook.common.service.master.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pubinfo.passbook.common.entity.TRuleChargeGenera;
import com.pubinfo.passbook.common.entity.TRuleDiseasesType;
import com.pubinfo.passbook.common.entity.TRuleReimbursable;
import com.pubinfo.passbook.common.mapper.TRuleChargeGeneraMapper;
import com.pubinfo.passbook.common.mapper.TRuleDiseasesTypeMapper;
import com.pubinfo.passbook.common.model.dto.rule.RuleChargeGeneraParam;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.service.master.TRuleChargeGeneraService;
import com.pubinfo.passbook.common.service.master.TRuleReimbursableService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 */
@Service
public class TRuleChargeGeneraServiceImpl extends ServiceImpl<TRuleChargeGeneraMapper, TRuleChargeGenera>
    implements TRuleChargeGeneraService {
  
  @Resource
  private TRuleReimbursableService reimbursableService;
  @Resource
  private TRuleDiseasesTypeMapper typeMapper;
  
  @Override
  public IPage<TRuleChargeGenera> selectChargeGenera(RuleChargeGeneraParam param) {
    IPage<TRuleChargeGenera> iPage = page(param.getPage("create_time"));
    return this.baseMapper.selectChargeGenera(iPage, param);
  }
  
  @Override
  public JsonResult<String> deleteChargeGenera(String generaId) {
    QueryWrapper<TRuleReimbursable> queryWrapper = new QueryWrapper();
    QueryWrapper<TRuleReimbursable> ruleReimbursables =
        queryWrapper.eq("charge_genera_id", generaId);
    List<TRuleReimbursable> list = reimbursableService.list(ruleReimbursables);
    if (!list.isEmpty())
      return JsonResult.error("该数据被可报销表引用,删除失败");
    this.baseMapper.deleteById(generaId);
    return JsonResult.success("删除成功");
  }
  
  @Override
  public JsonResult<String> saveChargeGenera(TRuleChargeGenera chargeGenera) {
    TRuleDiseasesType tRuleDiseasesType = typeMapper.selectById(chargeGenera.getDiseasesTypeId());
    if (tRuleDiseasesType == null)
      return JsonResult.error("病种类型id不存在");
    this.baseMapper.insert(chargeGenera);
    return JsonResult.success("保存成功");
  }
  
  @Override
  public JsonResult<String> updateChargeGenera(TRuleChargeGenera chargeGenera) {
    TRuleDiseasesType tRuleDiseasesType = typeMapper.selectById(chargeGenera.getDiseasesTypeId());
    if (tRuleDiseasesType == null)
      return JsonResult.error("病种类型id不存在");
    this.baseMapper.updateById(chargeGenera);
    return JsonResult.success("更新成功");
  }
}




